<?php

include_once 'equipement.class.php';

include_once 'except/tempEleveeChauffage.class.php';
include_once 'except/tempBasseChauffage.class.php';

include_once 'except/temperatureExt.class.php';

class Chauffage extends Equipement
{
	protected $etat;
	protected $temperature;
	protected $temperatureMax;
	protected $temperatureMin;

	public function __construct($m_ID, $m_etat, $m_temperature, $m_temperatureMax, $m_temperatureMin, $m_IDElement, $m_description, $m_nomPiece, $m_droit, $m_droitUtilisateur)
	{
		parent::__construct($m_ID, $m_IDElement, $m_description, $m_nomPiece, $m_droit, $m_droitUtilisateur);
		
		$this->etat = $m_etat;
		$this->temperature = $m_temperature;
		$this->temperatureMax = $m_temperatureMax;
		$this->temperatureMin = $m_temperatureMin;
	}

	public function __get($name)
	{
		if(property_exists(get_class(), $name))
		{
			return $this->$name;
		}
	}

	public function __set($nom, $valeur)
	{
		if($nom == "etat")
		{
			$this->etat = $valeur;
		}
		else if($nom == "temperature")
		{
			$this->temperature = $valeur;
		}
	}

	public function afficheEquipement()
	{
		echo"<div class='chauffage'>
				<p>Chauffage : ". $this->description. "</p>";
						if($this->droit < $this->droitUtilisateur)
						{
							echo "<p class='noDroit'>Vous n'avez le droit ne modifier cet équipement</p>";
						}
				echo "<div class='etat'>
				<label for='chauffEtat"  .$this->ID. "'>Etat : </label>
					<input type='radio' name='chauffEtat"  .$this->ID. "' value='on' "; 
						if($this->etat == "1")
						{
							echo "checked='checked' ";
						} 
						if($this->droit < $this->droitUtilisateur)
						{
							echo "readonly='readonly'";
						}
					echo "/>Allumé
					<input type='radio' name='chauffEtat"  .$this->ID. "' value='off' ";
						if($this->etat == "0")
						{
							echo "checked='checked' ";
						} 
						if($this->droit < $this->droitUtilisateur)
						{
							echo "readonly='readonly'";
						}
					echo "/>Eteint
				</div>
				<div class='temperature'>
				<label for='chauffTemp" .$this->ID. "'>Temperature : </label>
						<input type='number' min='" .$this->temperatureMin. "' max='" .$this->temperatureMax. "' step='0.5' value='" .$this->temperature. "' name='chauffTemp" .$this->ID. "' pattern='[0-9]*' ";
						if($this->droit < $this->droitUtilisateur)
						{
							echo "readonly='readonly'";
						}								
								echo ">
								</div>
								</div>";
	}
	
	public function afficheModifierEquipement()
	{
		echo "<input type='checkbox' name='suppChauffage" .$this->ID. "' value='true'>Supprimer le chauffage : " .$this->description;
	}

	public function majEquipement($tempExt = NULL)
	{
		if(isset($_POST['chauffTemp' .$this->ID]) && isset($_POST['chauffEtat' .$this->ID]) && ($_POST['chauffEtat' .$this->ID] != $this->etat || $_POST['chauffTemp' .$this->ID] != $this->temperature))
		{
			if(($_POST['chauffEtat' .$this->ID] == 'on') && $_POST['chauffTemp' .$this->ID] > $this->temperatureMax)
			{
				throw new TempEleveChauffage("Temperature maximale : " .$this->temperatureMax. " pour la piece : " .$this->nomPiece);
			}
			else if(($_POST['chauffEtat' .$this->ID] == 'on') && $_POST['chauffTemp' .$this->ID] < $this->temperatureMin)
			{
				throw new TempBasseChauffage("Temperature minimale : " .$this->temperatureMin. " pour la piece : " .$this->nomPiece);
			}
			else 
			{
				if($_POST['chauffEtat' .$this->ID] == 'on')
				{
					$valEtat = '1';
				}
								
				try 
				{
					$maj = SPDO::pdo()->prepare("UPDATE Chauffage SET Temperature = :temp, Etat = :etat WHERE ID_Chauffage = :id");
					$maj->execute(array('id' => $this->ID,
							'temp' => $_POST['chauffTemp' .$this->ID],
							'etat' => $valEtat));
				}
				catch (PDOException $e)
				{
					die($e->getMessage());
				}
			}
			
			if(!$this->etat && $_POST['chauffEtat' .$this->ID] == 'on' && ($_POST['chauffTemp' .$this->ID] <= $tempExt))
			{
				throw new TemperatureExt("La temperature extérieure est de " .$tempExt. "°C. Nous vous conseillons d\'ouvrir les fenêtres.");
			}
		}
	}
		
	public static function ajoutEquipement($m_temperatureMax, $m_temperatureMin, $m_descrition, $m_IDPiece, $m_droit)
	{
		try 
		{
			$insert = SPDO::pdo()->prepare("INSERT INTO Element(ID_Piece, Description, Droit) VALUES (:piece, :desc, :droit)");
			$insert->execute(array('piece' => $m_IDPiece,
					'desc' => $m_descrition,
					'droit' => $m_droit));
			
			$IDElement = SPDO::pdo()->lastInsertId();
			
			$insert = SPDO::pdo()->prepare("INSERT INTO Chauffage(TemperatureMax, TemperatureMin, ID_Element) VALUES (:tempMax, :tempMin, :id)");
			$insert->execute(array('tempMax' => $m_temperatureMax,
					'tempMin' => $m_temperatureMin,
					'id' =>  $IDElement));
		}
		catch (PDOException $e)
		{
			die($e->getMessage());
		}
	}
	
	public function supprimerEquipement()
	{
		if(isset($_POST['suppChauffage' .$this->ID]))
		{
			try
			{
				$delete = SPDO::pdo()->prepare("DELETE FROM Chauffage WHERE ID_Chauffage = :id");
				$delete->execute(array('id' => $this->ID));
			}
			catch (PDOException $e)
			{
				die($e->getMessage());
			}
			
			unset($_POST['suppChauffage' .$this->ID]);
		}
	}
}
